import os
import pathlib
root_path=pathlib.Path(__file__).parent
print(f"root path: {root_path}")
if not (root_path / "remotes").exists():
    (root_path / "remotes").touch()
for project in os.listdir(root_path):
    project_path=root_path/project
    if not project_path.is_dir():
        print(f"skipping {project} as this is not a directory...")
        continue
    git_path=project_path / ".git"
    if not git_path.is_dir():
        print(f"{project}: not a git directory")
        continue
    git_config_file=git_path/"config"
    if not git_config_file.exists():
        print(f"error: {project} does not exist")
        continue
    content=None
    with open(git_config_file,"r") as fp:
        content=fp.readlines()
    url:str=None
    remote_defined:bool=False
    for line in content:
        if "url" in line and remote_defined:
            url=line
            break
        if "remote" in line:
            remote_defined=True
            continue
    with open(root_path / "remotes.txt","a") as fp:
        if url:
            fp.write(project+" i.e. "+str(project_path)+"\n"+url)
        else:
            print(f"error: cannot find url in {project}" if remote_defined 
                else f"{project} has no remote ")
    